package com.logic.landseaserver.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpRequestClient
{
    private static final Logger LOG = LoggerFactory.getLogger(HttpRequestClient.class);
    
    public static String postHttpsRequest(String url, String bodyContent)
    {
        
        Protocol https = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443);
        Protocol.registerProtocol("https", https);
        PostMethod postMethod = new PostMethod(url);
        HttpClient httpClient = new HttpClient();
        postMethod.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        // 把参数值放入postMethod中
        postMethod.setRequestBody(bodyContent);
        String result = StringUtils.EMPTY;
        try
        {
            httpClient.executeMethod(postMethod);
            result = postMethod.getResponseBodyAsString();
            postMethod.releaseConnection();
        }
        catch (HttpException e)
        {
            LOG.error("postHttpsRequest error.", e);
        }
        catch (IOException e)
        {
            LOG.error("postHttpsRequest error.", e);
        }
        finally
        {
            postMethod.releaseConnection();
            if (httpClient != null)
            {
                ((SimpleHttpConnectionManager)httpClient.getHttpConnectionManager()).closeIdleConnections(0);
                httpClient = null;
            }
        }
        return result;
    }
    
    public static String postRequest(String url, String bodyContent, String headContent)
    {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestHeader("Connection", "close");
        postMethod.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
        if (!StringUtils.isEmpty(headContent))
        {
            postMethod.setRequestHeader("token", headContent);
        }
        postMethod.setRequestBody(bodyContent);
        // httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        String result = StringUtils.EMPTY;
        try
        {
            httpClient.executeMethod(postMethod);
            InputStream responStream = postMethod.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(responStream));
            String tempbf;
            StringBuffer html = new StringBuffer(100);
            while ((tempbf = br.readLine()) != null)
            {
                html.append(tempbf);
            }
            result = html.toString();
            
            postMethod.releaseConnection();
        }
        catch (HttpException e)
        {
            LOG.error("postRequest error.", e);
        }
        catch (IOException e)
        {
            LOG.error("postRequest error.", e);
        }
        finally
        {
            postMethod.releaseConnection();
            if (httpClient != null)
            {
                ((SimpleHttpConnectionManager)httpClient.getHttpConnectionManager()).closeIdleConnections(0);
                httpClient = null;
            }
        }
        return result;
    }
    
    public static String getRequest(String url, Map<String, String> headMap)
    {
        String result = null;
        HttpMethod method = new GetMethod(url);
        HttpClient client = new HttpClient();
        
        if (headMap != null)
        {
            for (String key : headMap.keySet())
            {
                method.addRequestHeader(key, headMap.get(key));
                
            }
        }
        
        try
        {
            client.executeMethod(method);
            result = method.getResponseBodyAsString();
            
        }
        catch (Exception e)
        {
            result = null;
            LOG.error("getRequest error.", e);
        }
        finally
        {
            method.releaseConnection();
            if (client != null)
            {
                ((SimpleHttpConnectionManager)client.getHttpConnectionManager()).closeIdleConnections(0);
                client = null;
            }
        }
        
        return result;
    }
    
}